package com.ssyc.gsk_master.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.utils.TextUtils;
import com.github.aakira.expandablelayout.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.Constants;
import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_master.R;
import com.ssyc.gsk_master.adapter.ClassMsgRvAdapter;
import com.ssyc.gsk_master.bean.ClassMsgInfo;
import com.ssyc.gsk_master.bean.MsgInfo;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 班级信息
 * Created by Administrator on 2018/7/17 0017.
 */

public class MasterClassMessageFragment extends LazyBaseFragment {

    private RecyclerView rv;
    private SmartRefreshLayout srf;
    private AVLoadingIndicatorView avi;
    private RelativeLayout rlLoading;

    //数据集合
    private List<MsgInfo> oldDatas;

    //老师账号和角色
    private String c, role;


    //空视图
    private View emptyView;
    private ClassMsgRvAdapter adapter;

    //单例模式
    private static class LazyHolder {
        private static final MasterClassMessageFragment INSTANCE = new MasterClassMessageFragment();
    }

    public static final MasterClassMessageFragment newInstance(String c, String role) {

        MasterClassMessageFragment masterClassMessageFragment = LazyHolder.INSTANCE;

        Bundle bundle = new Bundle();
        bundle.putString("c", c);
        bundle.putString("role", role);
        masterClassMessageFragment.setArguments(bundle);

        return masterClassMessageFragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.master_fragment_class_msg;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

        showContent();

        //初始化视图
        initView(view);

        //设置下拉刷新
        initSrf();

        //获取老师账号和角色
        initIntent();

        //请求服务器数据
        http();

    }


    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        rv = view.findViewById(R.id.rv);
        srf = view.findViewById(R.id.srf);
        avi = view.findViewById(R.id.avi);
        rlLoading = view.findViewById(R.id.rl_loading);
        oldDatas = new ArrayList<>();
    }


    /**
     * 设置下拉刷新
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, null);
    }

    /**
     * 获取老师账号和角色
     */
    private void initIntent() {
        role = getArguments().getString("role");
        c = getArguments().getString("c");

    }

    /**
     * 初始化列表
     */
    private void initRv() {
        emptyView = View.inflate(getContext(), R.layout.base_rv_common_empty_view_white_bg, null);
        adapter = new ClassMsgRvAdapter(getContext(),c, R.layout.master_rv_class_msg, oldDatas);
    }


    /**
     * 请求数据
     */
    private void http() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "29");
        map.put("c", c);
        map.put("role", role);
        HttpUtils.post(HttpAdress.MASTERBASEMSG, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    Log.i("test", "返回:" + response);
                    ClassMsgInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, ClassMsgInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("1".equals(info.getState())) {
                            //返回成功
                            //获取所有班级的名称
                            List<String> classNames = info.getClasses();

                            //组装数据
                            initData(info, classNames);

                            //初始化列表
                            initRv();

                            //设置适配器
                            BqaManager.setRv(emptyView, getContext(), adapter, rv);

                        } else {
                            UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 组装数据
     *
     * @param info
     * @param classNames
     */
    private void initData(ClassMsgInfo info, List<String> classNames) {

        MsgInfo msgInfo1 = new MsgInfo();
        ClassMsgInfo.Class1Bean class1 = info.getClass1();
        msgInfo1.className = classNames.get(0);
        msgInfo1.averageAttendance = class1.getAverageAttendance();
        msgInfo1.averageSelef = class1.getAverageSelef();
        msgInfo1.icon = class1.getIcon();
        msgInfo1.num = class1.getNum();
        msgInfo1.payyes = class1.getPayyes();
        msgInfo1.payno = class1.getPayno();
        msgInfo1.name = class1.getName();
        msgInfo1.averageExam = class1.getAverageExam();
        oldDatas.add(msgInfo1);

        MsgInfo msgInfo2 = new MsgInfo();
        ClassMsgInfo.Class2Bean class2 = info.getClass2();
        msgInfo2.className = classNames.get(1);
        msgInfo2.averageAttendance = class2.getAverageAttendance();
        msgInfo2.averageSelef = class2.getAverageSelef();
        msgInfo2.icon = class2.getIcon();
        msgInfo2.num = class2.getNum();
        msgInfo2.payyes = class2.getPayyes();
        msgInfo2.payno = class2.getPayno();
        msgInfo2.name = class2.getName();
        msgInfo2.averageExam = class2.getAverageExam();
        oldDatas.add(msgInfo2);

        MsgInfo msgInfo3 = new MsgInfo();
        ClassMsgInfo.Class3Bean class3 = info.getClass3();
        msgInfo3.className = classNames.get(2);
        msgInfo3.averageAttendance = class3.getAverageAttendance();
        msgInfo3.averageSelef = class3.getAverageSelef();
        msgInfo3.icon = class3.getIcon();
        msgInfo3.num = class3.getNum();
        msgInfo3.payyes = class3.getPayyes();
        msgInfo3.payno = class3.getPayno();
        msgInfo3.name = class3.getName();
        msgInfo3.averageExam = class3.getAverageExam();
        oldDatas.add(msgInfo3);


        MsgInfo msgInfo4 = new MsgInfo();
        ClassMsgInfo.Class4Bean class4 = info.getClass4();
        msgInfo4.className = classNames.get(3);
        msgInfo4.averageAttendance = class4.getAverageAttendance();
        msgInfo4.averageSelef = class4.getAverageSelef();
        msgInfo4.icon = class4.getIcon();
        msgInfo4.num = class4.getNum();
        msgInfo4.payyes = class4.getPayyes();
        msgInfo4.payno = class4.getPayno();
        msgInfo4.name = class4.getName();
        msgInfo4.averageExam = class4.getAverageExam();
        oldDatas.add(msgInfo4);


        MsgInfo msgInfo5 = new MsgInfo();
        ClassMsgInfo.Class5Bean class5 = info.getClass5();
        msgInfo5.className = classNames.get(4);
        msgInfo5.averageAttendance = class5.getAverageAttendance();
        msgInfo5.averageSelef = class5.getAverageSelef();
        msgInfo5.icon = class5.getIcon();
        msgInfo5.num = class5.getNum();
        msgInfo5.payyes = class5.getPayyes();
        msgInfo5.payno = class5.getPayno();
        msgInfo5.name = class5.getName();
        msgInfo5.averageExam = class5.getAverageExam();
        oldDatas.add(msgInfo5);

        MsgInfo msgInfo6 = new MsgInfo();
        ClassMsgInfo.Class6Bean class6 = info.getClass6();
        msgInfo6.className = classNames.get(5);
        msgInfo6.averageAttendance = class6.getAverageAttendance();
        msgInfo6.averageSelef = class6.getAverageSelef();
        msgInfo6.icon = class6.getIcon();
        msgInfo6.num = class6.getNum();
        msgInfo6.payyes = class6.getPayyes();
        msgInfo6.payno = class6.getPayno();
        msgInfo6.name = class6.getName();
        msgInfo6.averageExam = class6.getAverageExam();
        oldDatas.add(msgInfo6);

        MsgInfo msgInfo7 = new MsgInfo();
        ClassMsgInfo.Class7Bean class7 = info.getClass7();
        msgInfo7.className = classNames.get(6);
        msgInfo7.averageAttendance = class7.getAverageAttendance();
        msgInfo7.averageSelef = class7.getAverageSelef();
        msgInfo7.icon = class7.getIcon();
        msgInfo7.num = class7.getNum();
        msgInfo7.payyes = class7.getPayyes();
        msgInfo7.payno = class7.getPayno();
        msgInfo7.name = class7.getName();
        msgInfo7.averageExam = class7.getAverageExam();
        oldDatas.add(msgInfo7);

        MsgInfo msgInfo8 = new MsgInfo();
        ClassMsgInfo.Class8Bean class8 = info.getClass8();
        msgInfo8.className = classNames.get(7);
        msgInfo8.averageAttendance = class8.getAverageAttendance();
        msgInfo8.averageSelef = class8.getAverageSelef();
        msgInfo8.icon = class8.getIcon();
        msgInfo8.num = class8.getNum();
        msgInfo8.payyes = class8.getPayyes();
        msgInfo8.payno = class8.getPayno();
        msgInfo8.name = class8.getName();
        msgInfo8.averageExam = class8.getAverageExam();
        oldDatas.add(msgInfo8);


        MsgInfo msgInfo9 = new MsgInfo();
        ClassMsgInfo.Class9Bean class9 = info.getClass9();
        msgInfo9.className = classNames.get(8);
        msgInfo9.averageAttendance = class9.getAverageAttendance();
        msgInfo9.averageSelef = class9.getAverageSelef();
        msgInfo9.icon = class9.getIcon();
        msgInfo9.num = class9.getNum();
        msgInfo9.payyes = class9.getPayyes();
        msgInfo9.payno = class9.getPayno();
        msgInfo9.name = class9.getName();
        msgInfo9.averageExam = class9.getAverageExam();
        oldDatas.add(msgInfo9);

        MsgInfo msgInfo10 = new MsgInfo();
        ClassMsgInfo.Class10Bean class10 = info.getClass10();
        msgInfo10.className = classNames.get(9);
        msgInfo10.averageAttendance = class10.getAverageAttendance();
        msgInfo10.averageSelef = class10.getAverageSelef();
        msgInfo10.icon = class10.getIcon();
        msgInfo10.num = class10.getNum();
        msgInfo10.payyes = class10.getPayyes();
        msgInfo10.payno = class10.getPayno();
        msgInfo10.name = class10.getName();
        msgInfo10.averageExam = class10.getAverageExam();
        oldDatas.add(msgInfo10);

        //添加折叠动画效果
        for (int i = 0; i < oldDatas.size(); i++) {
            oldDatas.get(i).interpolator = Utils.createInterpolator(Utils.LINEAR_OUT_SLOW_IN_INTERPOLATOR);
        }
    }

}
